﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using SD.LLBLGen.Pro.ORMSupportClasses;
using DatabaseAdapter;
using DatabaseGeneric;
using DatabaseGeneric.FactoryClasses;
using DatabaseGeneric.EntityClasses;
using DatabaseGeneric.RelationClasses;
using DatabaseGeneric.HelperClasses;

namespace TKCCLD
{
    public partial class FormTimKiemSanPham : DevExpress.XtraEditors.XtraForm
    {
        public FormTimKiemSanPham()
        {
            InitializeComponent();
        }

        private FormMain formMain;
        public FormTimKiemSanPham(FormMain formMain)
        {
            InitializeComponent();
            this.formMain = formMain;
        }

        private Interfaces.ISanPham iSanPham = new Services.SanPham();
        private EntityCollection<SanPhamEntity> currentCollectionSanPham = null;

        private string strCriteria = "";
        private int intOption = 0;
        //FormLoad
        private void FormTimKiemSanPham_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'tKCNDataSet.SanPhamNew' table. You can move, or remove it, as needed.
            this.sanPhamNewTableAdapter.Fill(this.tKCNDataSet.SanPhamNew);
            checkPhot.Checked = true;
            lookUpEdit_Phot.Properties.DataSource = sanPhamNewBindingSource;
            lookUpEdit_Phot.Properties.DisplayMember = "Phot";
            lookUpEdit_Phot.Properties.ValueMember = "Phot";

            lookUpEdit_RapMai.Properties.DataSource = sanPhamNewBindingSource;
            lookUpEdit_RapMai.Properties.DisplayMember = "RapMai";
            lookUpEdit_RapMai.Properties.ValueMember = "RapMai";

            lookUpEdit_BanhMai.Properties.DataSource = sanPhamNewBindingSource;
            lookUpEdit_BanhMai.Properties.DisplayMember = "BanhMai";
            lookUpEdit_BanhMai.Properties.ValueMember = "BanhMai";

            lookUpEdit_Last.Properties.DataSource = sanPhamNewBindingSource;
            lookUpEdit_Last.Properties.DisplayMember = "Last";
            lookUpEdit_Last.Properties.ValueMember = "Last";

            lookUpEdit_KhuonMat.Properties.DataSource = sanPhamNewBindingSource;
            lookUpEdit_KhuonMat.Properties.DisplayMember = "KhuonMat";
            lookUpEdit_KhuonMat.Properties.ValueMember = "KhuonMat";
        }

        private void checkPhot_CheckedChanged(object sender, EventArgs e)
        {
            intOption = 1;
            if (checkPhot.Checked)
            {
                lookUpEdit_RapMai.Enabled = false;
                lookUpEdit_RapMai.EditValue = String.Empty;
                lookUpEdit_BanhMai.Enabled = false;
                lookUpEdit_BanhMai.EditValue = String.Empty;
                lookUpEdit_Last.Enabled = false;
                lookUpEdit_Last.EditValue = String.Empty;
                lookUpEdit_KhuonMat.Enabled = false;
                lookUpEdit_KhuonMat.EditValue = String.Empty;

                lookUpEdit_Phot.Enabled = true;
            }
        }

        private void checkRapMai_CheckedChanged(object sender, EventArgs e)
        {
            intOption = 2;
            if (checkRapMai.Checked)
            {
                lookUpEdit_Phot.Enabled = false;
                lookUpEdit_Phot.EditValue = String.Empty;
                lookUpEdit_BanhMai.Enabled = false;
                lookUpEdit_BanhMai.EditValue = String.Empty;
                lookUpEdit_Last.Enabled = false;
                lookUpEdit_Last.EditValue = String.Empty;
                lookUpEdit_KhuonMat.Enabled = false;
                lookUpEdit_KhuonMat.EditValue = String.Empty;

                lookUpEdit_RapMai.Enabled = true;
                lookUpEdit_RapMai.Focus();
            }
        }

        private void checkBanhMai_CheckedChanged(object sender, EventArgs e)
        {
            intOption = 3;
            if (checkBanhMai.Checked)
            {
                lookUpEdit_Phot.Enabled = false;
                lookUpEdit_Phot.EditValue = String.Empty;
                lookUpEdit_RapMai.Enabled = false;
                lookUpEdit_RapMai.EditValue = String.Empty;
                lookUpEdit_Last.Enabled = false;
                lookUpEdit_Last.EditValue = String.Empty;
                lookUpEdit_KhuonMat.Enabled = false;
                lookUpEdit_KhuonMat.EditValue = String.Empty;

                lookUpEdit_BanhMai.Enabled = true;
                lookUpEdit_BanhMai.Focus();
            }
        }

        private void checkLast_CheckedChanged(object sender, EventArgs e)
        {
            intOption = 4;
            if (checkLast.Checked)
            {
                lookUpEdit_Phot.Enabled = false;
                lookUpEdit_Phot.EditValue = String.Empty;
                lookUpEdit_RapMai.Enabled = false;
                lookUpEdit_RapMai.EditValue = String.Empty;
                lookUpEdit_BanhMai.Enabled = false;
                lookUpEdit_BanhMai.EditValue = String.Empty;
                lookUpEdit_KhuonMat.Enabled = false;
                lookUpEdit_KhuonMat.EditValue = String.Empty;

                lookUpEdit_Last.Enabled = true;
                lookUpEdit_Last.Focus();
                
            }
        }

        private void checkKhuonMat_CheckedChanged(object sender, EventArgs e)
        {
            intOption = 5;
            if (checkKhuonMat.Checked)
            {
                lookUpEdit_Phot.Enabled = false;
                lookUpEdit_Phot.EditValue = String.Empty;
                lookUpEdit_RapMai.Enabled = false;
                lookUpEdit_RapMai.EditValue = String.Empty;
                lookUpEdit_BanhMai.Enabled = false;
                lookUpEdit_BanhMai.EditValue = String.Empty;
                lookUpEdit_Last.Enabled = false;
                lookUpEdit_Last.EditValue = String.Empty;

                lookUpEdit_KhuonMat.Enabled = true;
                lookUpEdit_KhuonMat.Focus();
            }
        }
        //Phot
        private void lookUpEdit_Phot_EditValueChanged(object sender, EventArgs e)
        {
            //truyền data từ lookupedit sang textbox.
            //textPhot.Text = lookUpEdit_Phot.GetColumnValue("Phot").ToString();
                strCriteria = (string)lookUpEdit_Phot.EditValue;  
        }
        private void lookUpEdit_Phot_TextChanged(object sender, EventArgs e)
        {
            currentCollectionSanPham = iSanPham.FetchCollection(strCriteria, intOption);
            if (currentCollectionSanPham.Count == 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Không có dữ liệu !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                SourceSanPhamNew.DataSource = currentCollectionSanPham;
            }
        }   

        //RapMai
        private void lookUpEdit_RapMai_EditValueChanged(object sender, EventArgs e)
        {
            strCriteria = (string)lookUpEdit_RapMai.EditValue;
        }
        private void lookUpEdit_RapMai_TextChanged(object sender, EventArgs e)
        {
            currentCollectionSanPham = iSanPham.FetchCollection(strCriteria, intOption);
            SourceSanPhamNew.DataSource = currentCollectionSanPham;
        }

        //BanhMai
        private void lookUpEdit_BanhMai_EditValueChanged(object sender, EventArgs e)
        {
            strCriteria = (string)lookUpEdit_BanhMai.EditValue;
        }
        private void lookUpEdit_BanhMai_TextChanged(object sender, EventArgs e)
        {
            currentCollectionSanPham = iSanPham.FetchCollection(strCriteria, intOption);
            SourceSanPhamNew.DataSource = currentCollectionSanPham;
        }

        //Last
        private void lookUpEdit_Last_EditValueChanged(object sender, EventArgs e)
        {
            strCriteria = (string)lookUpEdit_Last.EditValue;
        }
        private void lookUpEdit_Last_TextChanged(object sender, EventArgs e)
        {
            currentCollectionSanPham = iSanPham.FetchCollection(strCriteria, intOption);
            SourceSanPhamNew.DataSource = currentCollectionSanPham;
        }

        //KhuonMat
        private void lookUpEdit_KhuonMat_EditValueChanged(object sender, EventArgs e)
        {
            strCriteria = (string)lookUpEdit_KhuonMat.EditValue;
        }
        private void lookUpEdit_KhuonMat_TextChanged(object sender, EventArgs e)
        {
            currentCollectionSanPham = iSanPham.FetchCollection(strCriteria, intOption);
            SourceSanPhamNew.DataSource = currentCollectionSanPham;
        }

        private void buttonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormTimKiemSanPham_FormClosed(object sender, FormClosedEventArgs e)
        {
            formMain.pFormTimKiemSanPham = null;
        }

        private void gridViewSanPham_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && !gridViewSanPham.IsFilterRow(e.RowHandle))
            {
                int rowHandle = e.RowHandle + 1;
                e.Info.DisplayText = rowHandle.ToString();
            }
        }
    }
}